package com.coffeeloversupportlibrary.coffeelover.mercury.main;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.coffeeloversupportlibrary.R;
import com.coffeeloversupportlibrary.coffeelover.mercury.entity.Type;
import com.coffeeloversupportlibrary.coffeelover.mercury.utils.HttpUtils;
import com.coffeeloversupportlibrary.coffeelover.mercury.utils.JsonUtils;
import com.google.gson.Gson;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;


import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * @author 盛守义
 * @createTime 2021/11/22 14:07
 * @projectName TurningCoffee
 * @className SelectPopupWindowActivity.java
 * @description TODO
 */
public class SelectPopupWindowActivity extends Activity implements View.OnClickListener {
    private LayoutInflater mInflater;

    private Button btn_reset, btn_submit;
    private LinearLayout layout;
    private RelativeLayout rl_category;
    private TagFlowLayout productionFlow, tasteFlow, brandFlow, bakingFlow;

    private String production, taste, brand, baking;
    private String[] types;
    private List<Type.DataDTO.TypeGroupDTO.SonTypesDTO> productionList, tasteList, brandList, bakingList;

    private HttpUtils httpUtils;
    private JsonUtils jsonUtils;
    private Type type;
    private List<Type.DataDTO.TypeGroupDTO> group;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.popup_menu);

        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        findView();
        getDatas();

    }

    void findView(){

        types = new String[4];
        mInflater = LayoutInflater.from(this);
        btn_submit = findViewById(R.id.btn_submitcate);
        btn_reset = findViewById(R.id.btn_reset);
        productionFlow = findViewById(R.id.production_flow);
        tasteFlow = findViewById(R.id.taste_flow);
        brandFlow = findViewById(R.id.brand_flow);
        bakingFlow = findViewById(R.id.baking_flow);
//        layout = findViewById(R.id.pop_layout);
//        layout.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//
//            }
//        });

        rl_category = findViewById(R.id.rl_popup);
        rl_category.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

        btn_submit.setOnClickListener(this);
        btn_reset.setOnClickListener(this);


        productionList = new ArrayList<>();
        tasteList = new ArrayList<>();
        brandList = new ArrayList<>();
        bakingList = new ArrayList<>();


        httpUtils = new HttpUtils();
        jsonUtils = new JsonUtils();
    }

    //获取类型
    public void getDatas(){
        Call call = httpUtils.getCall("/coffee/coffee-type/getTypeGroup");
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                SelectPopupWindowActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Log.e("返回信息", "失败");
                        Toast.makeText(SelectPopupWindowActivity.this, "网络连接崩溃了，绝对绝对不是我们的错", Toast.LENGTH_SHORT).show();
                    }
                });
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String res = response.body().string();
                type = new Gson().fromJson(res, Type.class);
                if(type.isSuccess() == true){
                    SelectPopupWindowActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            initData();
                        }
                    });
                }
            }
        });
    }

    //给flowlayout注入数据
    public void initData(){
        group = new ArrayList<>();
        group = type.getData().getTypeGroup();
        productionList = jsonUtils.getSonList("产地", group);
        tasteList = jsonUtils.getSonList("风味", group);
        brandList = jsonUtils.getSonList("品牌", group);
        bakingList = jsonUtils.getSonList("烘焙度", group);

        productionFlow.setAdapter(new TagAdapter<Type.DataDTO.TypeGroupDTO.SonTypesDTO>(productionList){

            @Override
            public View getView(FlowLayout parent, int position, Type.DataDTO.TypeGroupDTO.SonTypesDTO sonTypesDTO) {
                TextView tv = (TextView) mInflater.inflate(R.layout.category_flow_item, productionFlow, false);
                tv.setVisibility(View.VISIBLE);
                tv.setText(sonTypesDTO.getTypeName());
                return tv;
            }

            @Override
            public void onSelected(int position, View view) {
                super.onSelected(position, view);
                types[0] = productionList.get(position).getTypeName();
                Log.d("fengwei", "onSelected: "+position);
            }

            @Override
            public void unSelected(int position, View view) {
                super.unSelected(position, view);
                types[0] = null;
            }
        });
        tasteFlow.setAdapter(new TagAdapter<Type.DataDTO.TypeGroupDTO.SonTypesDTO>(tasteList) {

            @Override
            public View getView(FlowLayout parent, int position, Type.DataDTO.TypeGroupDTO.SonTypesDTO sonTypesDTO) {
                TextView tv = (TextView) mInflater.inflate(R.layout.category_flow_item, tasteFlow, false);
                tv.setVisibility(View.VISIBLE);
                tv.setText(sonTypesDTO.getTypeName());
                return tv;
            }

            @Override
            public void onSelected(int position, View view) {
                super.onSelected(position, view);
                types[1] = tasteList.get(position).getTypeName();
                Log.d("fengwei", "onSelected: "+position);
            }

            @Override
            public void unSelected(int position, View view) {
                super.unSelected(position, view);
                types[1] = null;
            }
        });
        brandFlow.setAdapter(new TagAdapter<Type.DataDTO.TypeGroupDTO.SonTypesDTO>(brandList) {

            @Override
            public View getView(FlowLayout parent, int position, Type.DataDTO.TypeGroupDTO.SonTypesDTO sonTypesDTO) {
                TextView tv = (TextView) mInflater.inflate(R.layout.category_flow_item, brandFlow, false);
                tv.setVisibility(View.VISIBLE);
                tv.setText(sonTypesDTO.getTypeName());
                return tv;
            }

            @Override
            public void onSelected(int position, View view) {
                super.onSelected(position, view);
                types[2] = brandList.get(position).getTypeName();
                Log.d("pinpai", "onSelected: "+position);
            }

            @Override
            public void unSelected(int position, View view) {
                super.unSelected(position, view);
                types[2] = null;
            }
        });
        bakingFlow.setAdapter(new TagAdapter<Type.DataDTO.TypeGroupDTO.SonTypesDTO>(bakingList) {

            @Override
            public View getView(FlowLayout parent, int position, Type.DataDTO.TypeGroupDTO.SonTypesDTO sonTypesDTO) {
                TextView tv = (TextView) mInflater.inflate(R.layout.category_flow_item, bakingFlow, false);
                tv.setVisibility(View.VISIBLE);
                tv.setText(sonTypesDTO.getTypeName());
                return tv;
            }

            @Override
            public void onSelected(int position, View view) {
                super.onSelected(position, view);
                types[3] = bakingList.get(position).getTypeName();
                Log.d("hongbei", "onSelected: "+position);
            }

            @Override
            public void unSelected(int position, View view) {
                super.unSelected(position, view);
                types[3] = null;
            }
        });

    }

    @Override
    public boolean onTouchEvent(MotionEvent event){
        finish();
        return true;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_reset:
                productionFlow.onChanged();
                tasteFlow.onChanged();
                brandFlow.onChanged();
                bakingFlow.onChanged();
                types = new String[4];
                break;

            case R.id.btn_submitcate:
                Intent intent = new Intent();
                intent.putExtra("types", types);
                setResult(RESULT_OK, intent);
                finish();
                break;
            default:
                break;
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }
}
